package com.hh.game.service.gateway.manager;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hh.game.common.base.exception.GameException;
import com.hh.game.common.base.utils.ObjectUtil;

public class RouteInfo {
    private String serviceName;
    private String handler;
    private String method;
    private String version;
    private String group;

    private JSONObject data;
    private String stringData;
    private byte[] bytes;
    private boolean isByte;
    private boolean isString;

    private RouteInfo() {
    }

    public String route() {
        return serviceName + "." + handler + "." + method;
    }

    public String getServiceName() {
        return serviceName;
    }

    public String getHandler() {
        return handler;
    }

    public JSONObject getData() {
        return data;
    }

    public String getStringData() {
        return stringData;
    }

    public byte[] getBytes() {
        return bytes;
    }

    public boolean isByte() {
        return isByte;
    }

    public boolean isString() {
        return isString;
    }

    public String getVersion() {
        return version;
    }

    public String getGroup() {
        return group;
    }

    public String getInterfaceName() {
        return MessageConstant.PACKAGE + "." + this.serviceName + ".service."
                + StrUtil.upperFirst(this.handler) + "Service";
    }

    public String getMethod() {
        return method;
    }

    @Override
    public String toString() {
        return "RouteInfo{" +
                "interfaceName='" + getInterfaceName() + '\'' +
                "serviceName='" + serviceName + '\'' +
                ", handler='" + handler + '\'' +
                ", method='" + method + '\'' +
                ", version='" + version + '\'' +
                ", group='" + group + '\'' +
                ", data='" + data + '\'' +
                '}';
    }


    public static class RouteInfoBuilder {
        private String serviceName;
        private String handler;
        private String method;
        private String version;
        private String group;
        private String data;
        private String stringData;
        private JSONObject jsonData;
        private byte[] bytes;
        private boolean isByte;
        private boolean isString;

        public RouteInfo build() {
            ObjectUtil.checkNull(serviceName, "serviceName");
            ObjectUtil.checkNull(handler, "handler");
            ObjectUtil.checkNull(method, "method");

            RouteInfo info = new RouteInfo();
            info.serviceName = this.serviceName;
            info.handler = this.handler;
            info.method = this.method;
            info.version = StrUtil.isBlank(this.version) ? MessageConstant.VERSION_DEFAULT : this.version;
            info.group = this.group;
            info.bytes = this.bytes;
            info.stringData = this.stringData;
            info.isString = this.isString;
            info.isByte = this.isByte;
            if (!this.isByte && !this.isString) {
                if (this.jsonData == null) {
                    try {
                        info.data = JSON.parseObject(data);
                    } catch (Exception e) {
                        throw new GameException(GameException.ILLEGAL_REQUEST, serviceName + "." + handler + "." + method,
                                String.format("[%s] is not a json data", data));
                    }
                } else {
                    info.data = jsonData;
                }
            }

            return info;
        }

        public RouteInfoBuilder serviceName(String serviceName) {
            this.serviceName = serviceName;
            return this;
        }

        public RouteInfoBuilder handler(String handler) {
            this.handler = handler;
            return this;
        }

        public RouteInfoBuilder method(String method) {
            this.method = method;
            return this;
        }

        public RouteInfoBuilder version(String version) {
            this.version = version;
            return this;
        }

        public RouteInfoBuilder group(String group) {
            this.group = group;
            return this;
        }

        public RouteInfoBuilder data(String data) {
            this.data = data;
            return this;
        }

        public RouteInfoBuilder data(JSONObject data) {
            this.jsonData = data;
            return this;
        }

        public RouteInfoBuilder bytes(byte[] bytes) {
            this.bytes = bytes;
            return this;
        }

        public RouteInfoBuilder stringData(String stringData) {
            this.stringData = stringData;
            return this;
        }

        public RouteInfoBuilder isString(boolean isString) {
            this.isString = isString;
            return this;
        }

        public RouteInfoBuilder isByte(boolean isByte) {
            this.isByte = isByte;
            return this;
        }
    }
}
